<template>
  <Layout :class="['layout', device]">
    <!-- SideMenu -->
    <Drawer
      v-if="isMobile()"
      placement="left"
      :wrapClassName="`drawer-sider ${navTheme}`"
      :closable="false"
      :value="collapsed"
      @on-visible-change="drawerClose"
    >
      <side-menu
        mode="vertical"
        :menus="menus"
        :theme="navTheme"
        @menuSelect="menuSelect"
      ></side-menu>
    </Drawer>

    <Sider
      :width="sideWith"
      hide-trigger
      v-else-if="isSideMenu()"
      collapsible
      :collapsed-width="collapsedWidth"
      v-model="collapsed"
    >
      <side-menu
        mode="vertical"
        :width="menuWidth"
        :menus="menus"
        :theme="navTheme"
        :collapsed="collapsed"
      ></side-menu>
    </Sider>

    <Layout
      :class="[layoutMode, `content-width-${contentWidth}`]"
      :style="{ paddingLeft: contentPaddingLeft, minHeight: '100vh' }">
      <!-- layout header -->
      <global-header
        :mode="layoutMode"
        :menus="menus"
        :theme="navTheme"
        :collapsed="collapsed"
        :device="device"
        @toggle="toggle"
      />

      <!-- layout content -->
      <Content class="content" :style="{ height: '100%', margin: '24px 24px 0', paddingTop: fixedHeader ? '64px' : '0' }">
        <multi-tab v-if="multiTab"></multi-tab>
        <transition name="page-transition">
          <route-view/>
        </transition>
      </Content>

      <!-- layout footer -->
      <Footer>
        <global-footer/>
      </Footer>

      <!-- Setting Drawer (show in development mode) -->
      <setting-drawer v-if="!production"></setting-drawer>
    </Layout>
  </Layout>

</template>

<script>
import { triggerWindowResizeEvent } from '@/utils/util'
import { mapState, mapActions } from 'vuex'
import { mixin, mixinDevice } from '@/utils/mixin'
import config from '@/config'

import RouteView from './RouteView'
import { MultiTab, GlobalHeader, GlobalFooter, SettingDrawer } from '@/components'
import SideMenu from '@/components/menu/SideMenu'

export default {
  name: 'BasicLayout',
  mixins: [mixin, mixinDevice],
  components: {
    RouteView,
    MultiTab,
    SideMenu,
    GlobalHeader,
    GlobalFooter,
    SettingDrawer
  },
  data () {
    return {
      production: config.production,
      collapsed: false,
      menus: [],
      sideWith: 220,
      collapsedWidth: 80
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.menu.addRouters,
    }),
    contentPaddingLeft () {
      return '0'
    },
    menuWidth () {
      return this.collapsed ? '80px' : '220px'
    },
    paddingCalc () {
      let left = ''
      if (this.sidebarOpened) {
        left = this.isDesktop() ? '220px' : '80px'
      } else {
        left = (this.isMobile() && '0') || ((this.fixSidebar && '80px') || '0')
      }
      return left
    }
  },
  watch: {
    sidebarOpened (val) {
      this.collapsed = !val
    }
  },
  created () {
    this.menus = this.mainMenu.find(item => item.path === '/').children
    this.collapsed = !this.sidebarOpened
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }
  },
  methods: {
    ...mapActions(['setSidebar']),
    toggle () {
      this.collapsed = !this.collapsed
      this.setSidebar(!this.collapsed)
      triggerWindowResizeEvent()
    },
    menuSelect () {
      if (!this.isDesktop()) {
        this.collapsed = false
      }
    },
    drawerClose () {
      this.collapsed = false
    }
  }
}
</script>

<style lang="less">
  @import url('../components/index.less');

  /*
   * The following styles are auto-applied to elements with
   * transition="page-transition" when their visibility is toggled
   * by Vue.js.
   *
   * You can easily play with the page transition by editing
   * these styles.
   */

  .page-transition-enter {
    opacity: 0;
  }

  .page-transition-leave-active {
    opacity: 0;
  }

  .page-transition-enter .page-transition-container,
  .page-transition-leave-active .page-transition-container {
    -webkit-transform: scale(1.1);
    transform: scale(1.1);
  }
</style>
